package com.github.rpc4j.producer.spring;

import org.springframework.beans.factory.FactoryBean;

import com.github.rpc4j.producer.core.Rpc4jProducerService;
import com.github.rpc4j.producer.service.Rpc4jService;
import com.github.rpc4j.producer.service.Rpc4jServicePool;
import com.github.rpc4j.producer.service.Service;

public class SpringRpc4jProducerBean implements FactoryBean {

	private String serviceInterface;

	private Object target;

	private String serviceVersion;

	private String serviceName;

	private String serviceDesc;

	private boolean asynCall;

	private int clientTimeout;

	private int clientIdleTimeout;

	private String serializeType;
	
	private Rpc4jProducerService rpc4jProducerService;
	
	private Service service ;
	
    public void setServiceInterface(String serviceInterface) {
		this.serviceInterface = serviceInterface;
	}

	public void setTarget(Object target) {
		this.target = target;
	}

	

	public void setServiceVersion(String serviceVersion) {
		this.serviceVersion = serviceVersion;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public void setServiceDesc(String serviceDesc) {
		this.serviceDesc = serviceDesc;
	}

	public void setAsynCall(boolean asynCall) {
		this.asynCall = asynCall;
	}

	public void setClientTimeout(int clientTimeout) {
		this.clientTimeout = clientTimeout;
	}

	public void setClientIdleTimeout(int clientIdleTimeout) {
		this.clientIdleTimeout = clientIdleTimeout;
	}

	public void setSerializeType(String serializeType) {
		this.serializeType = serializeType;
	}
	
	public void setRpc4jProducerService(Rpc4jProducerService rpc4jProducerService) {
		this.rpc4jProducerService = rpc4jProducerService;
	}
	

	@Override
	public Object getObject() throws Exception {
		this.service = new Service();
		service.setAsynCall(asynCall);
		service.setClientIdleTimeout(clientIdleTimeout);
		service.setClientTimeout(clientTimeout);
		service.setSerializeType(serializeType);
		service.setServiceDesc(serviceDesc);
		service.setServiceInterface(serviceInterface);
		service.setTarget(target);
		service.setServiceName(serviceName);
		service.setVersion(serviceVersion);
		service.init();
		return service;
	}
	
	public void init() throws Exception{
		rpc4jProducerService.getContext().getServicePool().addService((Service) getObject());
	}

	@Override
	public Class getObjectType() {
		return Rpc4jService.class;
	}

	@Override
	public boolean isSingleton() {
		return false;
	}

}
